iT邦幫忙

2022 iThome 鐵人賽

DAY 19
0
Modern Web

clojure 刷刷鍋系列 第 19

Clojure 肉片 -第 19 塊

  • 分享至 

  • xImage
  •  

【今日湯底】

Complete the function/method so that it returns the url with anything after the anchor (#) removed.

Examples
"www.codewars.com#about" --> "www.codewars.com"
"www.codewars.com?page=1" -->"www.codewars.com?page=1"

(必須通過以下測試)

(ns kata.test
  (:require [clojure.test :refer :all]
            [kata         :refer [remove-url-anchor]]))

(deftest basic-tests
  (is (= (remove-url-anchor "www.codewars.com#about") "www.codewars.com"))
  (is (= (remove-url-anchor "www.codewars.com/katas/?page=1#about") "www.codewars.com/katas/?page=1"))
  (is (= (remove-url-anchor "www.codewars.com/katas/") "www.codewars.com/katas/")))

【我的答案】

(ns kata)
(defn remove-url-anchor [url]
  (first (clojure.string/split url #"#"))
  )

【其他人的答案】

(ns kata)

(defn remove-url-anchor [url]
  (clojure.string/replace url #"#.*" "")
)
(ns kata)

(defn remove-url-anchor [url]
  (if (= -1 (.indexOf url "#"))
    (subs url 0)
    (subs url 0 (.indexOf url "#"))
  )
  )

上一篇
Clojure 肉片 -第 18 塊
下一篇
Clojure 肉片 -第 20 塊
系列文
clojure 刷刷鍋30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言